﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace SuperSnake
{
    class GameField
    {
        public static int Width;//场景宽，以方块大小为单位
        public static int Height;//场景高
        public static Color backColor;//场景背景色
        public static IntPtr winHandle; //场景句柄
        public static int blockWidth=20;//方块大小，场景中的最小单位
        public static Color blockColor = SystemColors.Control;//方块颜色
        public static int snakeLength = 5;//初始时蛇长度
        public static foodblock food;//当前生成的食物
        public static bool foodVisible=false;//食物是否可见
        
        /*检测该位置是否为空*/
        public static bool isEmpty(int x,int y,Snake snakeSelf)
        {
            if (x < 0 || y < 0 || x >=Width * blockWidth || y >=Height * blockWidth)
                return false;
            for (int i = 3; i <=snakeSelf.snake.Count - 2; i++)
            {
                Block tempblock=(Block)snakeSelf.snake[i];
                if (tempblock.location.X == x && tempblock.location.Y == y)
                    return false;
            }
            return true;
        }
        /*检测下一个位置是否有食物*/
        public static bool haveFood(int x,int y)
        {
            if (food == null||foodVisible==false)
                return false;
            else if (food.location.X == x && food.location.Y == y)
                return true;
            else
                return false;
        }
        /*吃掉食物之后，随机生成下一个食物*/
        public static void createFood(Snake snakeSelf)
        {
            bool onSnakeSelf=false;
            Point location;
            do
            {
                onSnakeSelf = false;
                location = randLocation();
                foreach (object obj in snakeSelf.snake)
                {
                    Block b = (Block)obj;
                    if (b.location == location)
                    { onSnakeSelf = true; break; }
                }
            } while (onSnakeSelf);
            foodblock bb = new foodblock(location);
            GameField.food = bb;
        }
        /*随机生成位置*/
        public static Point randLocation()
        {
            Random rand = new Random();
            int x = rand.Next(Width);
            int y = rand.Next(Height);
            Point loc = new Point(x * blockWidth, y * blockWidth);
            return loc;
        }
    }
}
